import 'package:flutter/material.dart';

import 'package:flutter_ume_kit_mock/data/mock_config_manager.dart';
import 'package:flutter_ume_kit_mock/data/model/mock.dart';
import 'package:flutter_ume_kit_mock/widgets/mock_state.dart';

class MockItem extends StatelessWidget {
  final Mock mock;
  final MockClickListener mockClickListener;

  const MockItem(this.mock, this.mockClickListener, {Key? key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Ink(
      color: mock.enable ? Colors.yellow.shade300 : Colors.lightBlue.shade50,
      child: InkWell(
        overlayColor: MaterialStateProperty.all(Colors.lightBlue.withOpacity(0.1)),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                //标题
                Expanded(
                    child: Column(
                  children: [_mockTitleText(), _mockRequestText()],
                  crossAxisAlignment: CrossAxisAlignment.start,
                )),
                //开关
                _switchButton(),
              ],
            ),
            //描述
            if (mock.fields?.desc != null) _mockDescText(),
            SizedBox(height: 10),
            SizedBox(
              height: 1,
              width: double.infinity,
              child: ColoredBox(
                color: Colors.grey.shade300,
              ),
            )
          ],
        ),
        onTap: () {
          mockClickListener(mock);
        },
      ),
    );
  }

  ///标题
  Widget _mockTitleText() {
    return Padding(
      padding: const EdgeInsets.only(left: 16),
      child: Text(
        "${mock.fields?.title}",
        overflow: TextOverflow.ellipsis,
        style: TextStyle(
            color: Colors.black, fontSize: 14, fontWeight: FontWeight.bold),
      ),
    );
  }

  ///描述
  Widget _mockDescText() {
    return Padding(
      padding: EdgeInsets.only(left: 16, right: 16),
      child: Text(
        mock.fields!.desc!,
        style: TextStyle(color: Colors.grey.shade700, fontSize: 12),
      ),
    );
  }

  ///请求
  Widget _mockRequestText() {
    return Padding(
      padding: const EdgeInsets.only(left: 16, top: 1),
      child: Row(
        children: [
          Container(
              margin: EdgeInsets.only(right: 5),
              decoration: BoxDecoration(
                  color: Colors.orange.shade300,
                  borderRadius: BorderRadius.all(Radius.circular(6))),
              padding: EdgeInsets.only(left: 6, right: 6, top: 1, bottom: 1),
              alignment: Alignment.center,
              child: Text(
                "${mock.request?.fields?.method}",
                style: TextStyle(
                    color: Colors.white,
                    fontSize: 10,
                    fontWeight: FontWeight.bold),
                strutStyle: StrutStyle(leading: 0, forceStrutHeight: true),
              )),
          Expanded(
            child: Text(
              mock.request?.fields?.path ?? "",
              style: TextStyle(
                  overflow: TextOverflow.ellipsis,
                  color: Colors.grey.shade700,
                  fontSize: 12,
                  fontWeight: FontWeight.bold),
            ),
          ),
        ],
      ),
    );
  }

  ///开关
  Widget _switchButton() {
    return Switch(
        value: mock.enable,
        onChanged: (value) {
          if (value && MockConfigManager.checkEnableConflict(mock: mock)) {
            //冲突
            return;
          }
          mock.enable = value;
          if (value) {
            MockConfigManager.addEnable(mock.id);
          } else {
            MockConfigManager.removeEnable(mock.id);
          }
          MockConfigManager.mockListNotifier.notifyListeners();
        });
  }
}
